package com.yyq.controller;

import com.yyq.utils.upFile;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.Setter;
import org.pomo.toasterfx.ToastBarToasterService;
import org.pomo.toasterfx.model.ToastParameter;
import org.pomo.toasterfx.model.ToastType;
import org.pomo.toasterfx.model.impl.RandomAudio;
import org.pomo.toasterfx.model.impl.ToastTypes;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * @author yyq
 * @create 2021-10-06 21:25
 */
public class FirController implements Initializable {
    // 你可以在任何线程中，调用此方法。
    static ToastBarToasterService service = new ToastBarToasterService();
    static {
        service.initialize();
    }




    @FXML
    Label text;

    public static Scene getSecene() throws IOException {
        URL xmlURL=FirController.class.getResource("/fxml/test.fxml");
        AnchorPane ap= FXMLLoader.load(xmlURL);
        return new Scene(ap);


    }

    public static void sc() throws IOException {
        //获取剪贴板和设置剪贴板的内容
        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent clipboardContent=new ClipboardContent();

        //获取图像保
        if(clipboard.hasImage()){
            Image image = clipboard.getImage();
            BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
            File file = new File("D:/image.png");
            //保存图像
            ImageIO.write(bImage, "png", file);
            //上传图像获取URL 后删除图像
            String imgURL = upFile.upload(file);
            file.delete();

            System.out.println(imgURL);

            //设置剪贴板内容为imgURL
            //10/21修改:修改格式为![](imgURL)
            clipboardContent.put(DataFormat.PLAIN_TEXT,"![]("+imgURL+")");
            clipboard.setContent(clipboardContent);
            service.success("替换成功", "已替换为URL:"+imgURL,
                    ToastParameter.builder().timeout(Duration.seconds(3)).build());
        }
        else {
            // 你可以在任何线程中，调用此方法。
            service.warn("剪贴板没有图片", "请重新截图",
                    ToastParameter.builder().timeout(Duration.seconds(3)).build());

        }

    }

    @FXML
    public void alert() throws IOException, InterruptedException {
        System.out.println("剪贴板的内容---");
        sc();



    }

    public void initialize(URL location, ResourceBundle resources) {

    }
}
